package com.atwisdom.star.service.util;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.atwisdom.star.common.util.JsonUtil;
import com.atwisdom.star.common.util.UuidTool;
import com.atwisdom.star.core.dao.pojo.modelClass.resource.ResourceInstance;
import com.atwisdom.star.core.dsl.modelClass.resource.Impl.ResourceInstanceDslServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class ResourceUtil {
    @Autowired
    private ResourceInstanceDslServiceImpl resourceInstanceDslServiceImpl;

    /**
     * 复制实例
     * @param id
     * @return
     */
    public JSONObject copyIns(Long id,JSONObject newValues){
        ResourceInstance ins = resourceInstanceDslServiceImpl.queryInsById(id);
        if(null==ins){
            return null;
        }
        JSONObject insJson = resourceInstanceDslServiceImpl.getResourceInsJsonObjectList(Arrays.asList(ins)).get(0);
        JSONObject values = insJson.getJSONObject("values");
//        values.put("if_template",ins.getId());
//        values.put("bf_template",0);
        values.putAll(newValues);
        return resourceInstanceDslServiceImpl.add(ins.getType(),ins.getClassName(),UuidTool.getUUID32(),values,ins.getWeb());
    }
    /**
     * 根据条件查询符合则返回不符合则新增返回
     * @param className 对象名称
     * @param type 对象类型
     * @param values 对象字段
     * @return 查询或新增实例
     */
    public JSONObject queryUpdateOrAdd(String className, String type, JSONObject values, String ...linkKeys){
        String condition = "className=='"+className+"'&&type=='"+type+"'";
        if(ObjectUtil.isAllNotEmpty(linkKeys)){
            for (String LinkKey:linkKeys){
                if(values.get(LinkKey) instanceof String){
                    condition = condition + "&&"+LinkKey+"=='"+values.get(LinkKey)+"'";
                }else{
                    condition = condition + "&&"+LinkKey+"=="+values.get(LinkKey)+" ";
                }
            }
        }
        List<JSONObject> insList = this.resourceInstanceDslServiceImpl.query(condition,className,null);
        if(insList.size()>0){
            JSONObject ins = insList.get(0);
            boolean updateFlag = false;
            for(String key:values.keySet()){
                if(ObjectUtil.isNotEmpty(values.get(key))&&values.get(key)!=ins.getJSONObject("values").getString(key)){
                    updateFlag = true;
                    break;
                } else if (ObjectUtil.isEmpty(values.get(key)) && ObjectUtil.isNotEmpty(ins.getJSONObject("values").get(key))) {
                    updateFlag = true;
                    break;
                }
            }
            if(updateFlag){
                return resourceInstanceDslServiceImpl.updateById(JsonUtil.buildJson(new Object[] {"id",ins.getLong("id"),"className",className,"type",type,"values",values}));
            }
            return ins;
        }else{
            return this.resourceInstanceDslServiceImpl.add(type,className,String.valueOf(System.currentTimeMillis()),values);
        }
    }
}
